package com.fndsoft.police.xuancheng.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fndsoft.police.xuancheng.R;
import com.fndsoft.police.xuancheng.util.ViewUtil;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * @ClassName: TitleBar
 * @Description: 所有界面的TitleBar
 * @author scofieldandroid@gmail.com
 * @date 2012-11-1 上午9:41:49
 * 
 */
public class TitleBar extends RelativeLayout {

	@ViewInject(R.id.rlNavLeft)
	public RelativeLayout rlNavLeft;// 左边的按钮区域
	@ViewInject(R.id.btnTitleBarLeft)
	public ImageButton leftBtn;// 左边的按钮
	@ViewInject(R.id.tvTitle)
	public TextView centerTextView;// 中间的文本
	@ViewInject(R.id.tvSubTitle)
	private TextView centerSubTextView;// 副标题
	@ViewInject(R.id.btnTitleBarRight1)
	public ImageButton rightBtn1;// 右边的按钮
	@ViewInject(R.id.btnTitleBarRight2)
	public ImageButton rightBtn2;// 右边的按钮
	@ViewInject(R.id.btnTitleBarRight3)
	public ImageButton rightBtn3;// 右边的按钮

	public TitleBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public TitleBar(Context context) {
		super(context);
		init(context);
	}

	// ======================================
	private Context mContext;

	private void init(Context context) {
		this.mContext = context;
		LayoutInflater mInflater = LayoutInflater.from(mContext);
		mInflater.inflate(R.layout.titlebar_two_btn_and_title, this);
		ViewUtils.inject(this, this);
		ViewUtil.setViewsGone(centerSubTextView, leftBtn, rightBtn1, rightBtn2,
				rightBtn3);
	}

	public void initSyle(TitleSyle titleSyle, String title) {
		centerTextView.setText(title);
		switch (titleSyle) {
		case LeftBtn:
			ViewUtil.setViewVisible(leftBtn, View.VISIBLE);
			break;
		case RightBtn:
			ViewUtil.setViewsVisible(rightBtn1);
			break;
		case NoBtn:

			break;
		case TwoBtn:
			ViewUtil.setViewsVisible(leftBtn, rightBtn1);
			break;
		case ThreeBtn:// 三个按钮
			ViewUtil.setViewsVisible(leftBtn, rightBtn1, rightBtn2);
			break;
		case FourBtn:// 四个按钮
			ViewUtil.setViewsVisible(leftBtn, rightBtn1, rightBtn2, rightBtn3);
			break;
		default:
			break;
		}
	}

	public enum TitleSyle {
		LeftBtn, RightBtn, NoBtn, TwoBtn, ThreeBtn, FourBtn;
	}

	/**
	 * 重新定制新的
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：2014年3月22日 下午10:54:53
	 * @param layoutId
	 */
	public void setCustomView(int layoutId) {
		this.removeAllViews();
		LayoutInflater mInflater = LayoutInflater.from(mContext);
		View customView = mInflater.inflate(layoutId, this);
		ViewUtils.inject(this, customView);
		ViewUtil.setViewsGone(leftBtn, rightBtn1, rightBtn2, rightBtn3);
	}

	public void setLeftBtnOnclick(View.OnClickListener onClickListener) {
		leftBtn.setOnClickListener(onClickListener);
		rlNavLeft.setOnClickListener(onClickListener);
		centerTextView.setOnClickListener(onClickListener);
		centerSubTextView.setOnClickListener(onClickListener);
	}

	public void setRightBtnOnclick(View.OnClickListener onClickListener) {
		rightBtn1.setOnClickListener(onClickListener);
	}

	/**
	 * 让标题居中
	 * 
	 * @author carlos carlosk@163.com
	 * @version 创建时间：Mar 16, 2014 3:43:55 PM
	 */
	public void setTitleCenter() {
		LinearLayout titleLayout = (LinearLayout) findViewById(R.id.llTitle);
		RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) titleLayout
				.getLayoutParams();
		// 距离左边取消
		layoutParams.leftMargin = 0;
		// 居中
		layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
		titleLayout.setLayoutParams(layoutParams);
	}

	public void setSubTitle(String title) {
		centerSubTextView.setText(title);
		ViewUtil.setViewsVisible(centerSubTextView);
	}

}
